package com.yc.spring.jdbc;

import org.apache.commons.dbcp.BasicDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.annotation.Resource;
import javax.sql.DataSource;

@Configuration
@ComponentScan("com.yc.spring.jdbc")
@EnableTransactionManagement
//@EnableAspectJAutoProxy
public class JdbcConfig {

    @Bean("dataSrouce")
    public DataSource getDataSource() {
        BasicDataSource bds = new BasicDataSource();
        bds.setDriverClassName("com.mysql.jdbc.Driver");
        bds.setUrl("jdbc:mysql://127.0.0.1/c94-s3-ply-spring-jdbc");
        bds.setUsername("root");
        bds.setPassword("a");
        return bds;
    }

    @Resource
    DataSource dataSource;

    @Bean("jdbcTemplate")
    public JdbcTemplate getJdbcTemplate(){
        JdbcTemplate jt = new JdbcTemplate(dataSource);
        return jt;
    }

    /**
     * 定义数据事务管理器
     * @return
     */
    @Bean
    public DataSourceTransactionManager dataSourceTransactionManager(){
        return new DataSourceTransactionManager(dataSource);
    }

}
